package com.liangtian.mdm.material.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liangtian.mdm.dm.jsp.common.ExcelUtil;
import com.liangtian.mdm.material.common.util.ExcelExportUtil;
import com.liangtian.mdm.material.dao.WlbmLbJldwDao;
import com.liangtian.mdm.material.model.WlbmLbJldw;
import com.liangtian.mdm.material.service.WlbmLbJldwService;

@SuppressWarnings({"unchecked", "rawtypes"})
@Service
public class WlbmLbJldwServiceImpl implements WlbmLbJldwService {

    @Autowired
    private WlbmLbJldwDao wlbmLbJldwDao;

    @Override
    public PageInfo getWlbmLbJldwByCond(Map<String, Object> map) {
        PageInfo<List<WlbmLbJldw>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）
            List<WlbmLbJldw> list = wlbmLbJldwDao.getWlbmLbJldwByCond(map);
            pageInfo = new PageInfo(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public int insertWlbmLbJldw(WlbmLbJldw lbjldw) {
        return wlbmLbJldwDao.insertWlbmLbJldw(lbjldw);
    }

    @Override
    public int checkWlbmLbJldwExist(WlbmLbJldw lbjldw) {
        int i = 0;
        i = wlbmLbJldwDao.checkWlbmLbJldwExist(lbjldw);
        return i;
    }

    @Override
    public int deleteWlbmLbJldwByIds(Map<String, Object> map) {
        return wlbmLbJldwDao.deleteWlbmLbJldwByIds(map);
    }

    @Override
    public List<WlbmLbJldw> getWlbmLbJldwByCond2(Map<String, Object> cond) {
        return wlbmLbJldwDao.getWlbmLbJldwByCond(cond);
    }

    @Override
    public int getDelCountByJldwId(Map<String, Object> map) {
        return wlbmLbJldwDao.getDelCountByJldwId(map);
    }

    @Override
    public void exportWlbmLbJldw(List<WlbmLbJldw> list, HttpServletResponse response) {
        try {
            String tablename = "物料类别计量单位";
            // 声明一个工作薄
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("物料类别计量单位");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = null;
            CellStyle style = ExcelUtil.createCellTextStyle(workbook);
            cell = row.createCell(0);
            sheet.setDefaultColumnStyle(0, style);
            cell.setCellValue("类别代码");
            cell = row.createCell(1);
            sheet.setDefaultColumnStyle(1, style);
            cell.setCellValue("类别名称");
            cell = row.createCell(2);
            sheet.setDefaultColumnStyle(2, style);
            cell.setCellValue("计量单位");
            cell = row.createCell(3);
            sheet.setDefaultColumnStyle(3, style);
            cell.setCellValue("单位符号");
            cell = row.createCell(4);
            sheet.setDefaultColumnStyle(4, style);
            cell.setCellValue("说明");
            for (int i = 0; i < list.size(); i++) {
                WlbmLbJldw getWlLb = list.get(i);
                row = sheet.createRow(i + 1);
                cell = row.createCell(0);
                cell.setCellValue(getWlLb.getLbbm());
                cell = row.createCell(1);
                cell.setCellValue(getWlLb.getLbmc());
                cell = row.createCell(2);
                cell.setCellValue(getWlLb.getMc());
                cell = row.createCell(3);
                cell.setCellValue(getWlLb.getFh());
                cell = row.createCell(4);
                cell.setCellValue(getWlLb.getSm());
            }
            ExcelExportUtil.outExportExcel(tablename, workbook, response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public PageInfo getWlbmLbJldwByCond3(Map<String, Object> map) {
        PageInfo<List<WlbmLbJldw>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）
            List<WlbmLbJldw> list = wlbmLbJldwDao.getWlbmLbJldwByCond(map);
            pageInfo = new PageInfo(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

}
